<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WriteOff;
use App\Models\Order;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Storage;

class WriteOffController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WriteOff(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('wouser_id')->display(function ($id) {
                return User::find($id)->name ?? '';
            });
            $grid->column('user_id')->display(function ($id) {
                return User::find($id)->name ?? '';
            });
            $grid->column('sell_type')->using(Order::sellTypeMap());
            $grid->column('sell_json')->display(function ($value) {
                $sellJson = json_decode($value, true);
                $img = Storage::url($sellJson['img'] ?? '');
                return "<div>{$sellJson['name']}</div><img src={$img} width='50'>";
            });
            $grid->column('order_sn');
            $grid->column('pay_price');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('wouser_id')->select(User::pluck('name', 'id'));
                $filter->equal('user_id')->select(User::pluck('name', 'id'));
                $filter->equal('sell_type')->select(Order::sellTypeMap());
                $filter->where('sell_json', function ($query) {
                    $query->where('sell_json->name', 'like', "%{$this->input}%");
                });
                $filter->equal('pay_type')->select(Order::payTypeMap());
                $filter->like('order_sn');
                $filter->between('pay_price');
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'order_sn']);
            // 禁用
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            // 导出
            $grid->export()->rows(function ($rows) {
                $userNames = User::pluck('name', 'id');
                $sellType = Order::sellTypeMap();
                foreach ($rows as $index => &$row) {
                    $row['wouser_id'] = $userNames[$row['wouser_id']] ?? '';
                    $row['user_id'] = $userNames[$row['user_id']] ?? '';
                    $sellJson = json_decode($row['sell_json'], true) ?? [];
                    $row['sell_type'] = $sellType[$row['sell_type']];
                    $row['sell_json'] = $sellJson['name'] ?? '';
                }

                return $rows;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WriteOff(), function (Show $show) {
            $show->field('id');
            $show->field('wouser_id');
            $show->field('user_id');
            $show->field('camp_id');
            $show->field('order_id');
            $show->field('sell_type');
            $show->field('sell_id');
            $show->field('order_sn');
            $show->field('pay_price');
            $show->field('sell_json');
            $show->field('created_at');
            $show->field('updated_at');
            // 禁用
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WriteOff(), function (Form $form) {
            $form->display('id');
            $form->text('wouser_id');
            $form->text('user_id');
            $form->text('camp_id');
            $form->text('order_id');
            $form->text('sell_type');
            $form->text('sell_id');
            $form->text('order_sn');
            $form->text('pay_price');
            $form->text('sell_json');
        });
    }
}
